package com.newly.center.sys.api;

import com.newly.api.sys.feign.AuthClient;
import com.newly.center.sys.service.auth.api.IAuthService;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.sys.api.ApiVo;
import com.newly.common.base.entity.sys.captcha.dto.CaptchaDto;
import com.newly.common.base.entity.sys.captcha.vo.CaptchaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author gjm guan
 * @date 2021/9/12 10:28
 * @desc 鉴权api
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_SYS_AUTH)
public class AuthApi implements AuthClient {

    @Autowired
    private IAuthService authService;

    @Override
    public R logout() {
        boolean result = false;
        try {
            result = authService.logout();
        } catch (Exception e) {
            e.printStackTrace();
            R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @Override
    public R captcha() {
        CaptchaVo captchaVo = null;
        try {
            captchaVo = authService.generateCaptcha();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(captchaVo);
    }

    @Override
    public R checkCaptcha(CaptchaDto captchaDto) {
        String captchaId = captchaDto.getCaptchaId();
        String code = captchaDto.getCode();
        boolean result = false;
        try {
            result = authService.checkCaptcha(captchaId, code);
        } catch (Exception e) {
            e.printStackTrace();
            R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @Override
    public R queryApis() {
        List<ApiVo> apiVos = null;
        try {
            apiVos = authService.getApis();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(apiVos);
    }

}
